<?php 
/*
Template Name: process add user
*/
?>
<?php 
	//include_once($_SERVER['DOCUMENT_ROOT'].'/TTNaNoNangLuong/wp-load.php' );
	global  $user_id;
	global $wpdb;
	function hask_password($password) 
		{
			  
			  $hash_p = wp_hash_password( trim( $password ) );
			  return $hash_p;
		}
?>
<?php 
    if ( ! current_user_can( 'list_users' ) ) 
    {
      // echo 'die'; return;
      wp_die( __( 'bạn không có quyền truy cập trang này! ' ) );

            global $current_user;
            get_currentuserinfo();
    }//if ( ! current_user_can( 'list_users' ) ) 
    else {  
            ?>
<?php if (isset($_POST['createuser']) and $_POST['createuser']=='createuser')
	{
			
		if($_POST)
		{
			/*
			echo 'cac post';
			var_dump($_POST);
			return;
			*/

		  if ($_POST['role'] == 'administrator') 
			{
				 
							$name_full_user = $_POST['first_name'].$_POST['last_name'];
							$user_registered = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
							$pass_p = hask_password($_POST['pass1']);

							 $username = $_POST['user_login'];
					       if ( username_exists( $username ) and email_exists($_POST['email']) )
					       {
					           echo "Tên hoặc email này đã có người sử dụng!"; return;
					       }else
					       	{  
					       		$wpdb->insert( 
										'wp_users', 
										array( 
										'user_login' => $_POST['user_login'],
										'user_pass' => $pass_p,
										'user_nicename' => $_POST['user_login'],
										'user_email' => $_POST['email'],
										'user_url' => $_POST['url'],
										'user_registered' => $user_registered,
										'user_activation_key' => null,
										'user_status' => 0,
										'display_name' => $name_full_user,
										), 
										array( 
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
										) 
								);

							       // lay ID user vừa chen vào 
									// chen ID user vao bang wp_usermeta					
										$user = get_user_by( 'email', $_POST['email'] );
								        $user_id= $user->ID ;

								        //echo 'them 1 nguoi dung';
								        //var_dump($user_id);
								        //return;

								     // insert trruong du lieu ( wp_capabilities ) 1 :12 
									$wp_capabilities = 'wp_capabilities';
									$wp_capabilities_value = 'a:1:{s:13:"administrator";b:1;}';
									$wp_user_level = 'wp_user_level';
									$wp_user_level_val = '10';

									$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
											VALUES (%d, %d, %s, %s ) ", 
									        array(
									        	NULL,
											$user_id, 
											$wp_capabilities,
											$wp_capabilities_value ) 
									    ) );

									$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
											VALUES (%d, %d, %s, %s ) ", 
									        array(
									        	NULL,
											$user_id, 
											$wp_user_level,
											$wp_user_level_val ) 
									    ) );
									
									   // update lai lan nua cho chac an
									/*

										$wpdb->query( " UPDATE wp_usermeta 
														WHERE wp_usermeta.user_id = wp_users.ID "
													);
									*/
								echo "<h1>Thêm người dùng thành công</h1>";
								return;
			                }//elseif($_POST['email']) 
				        /*
				       
						INSERT INTO wp_usermeta (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '4', 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');


						INSERT INTO `databasename`.`wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '4', 'wp_user_level', '10');
				       
						INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) 
										VALUES
																			(1, 1, 'first_name', ''),
																			(2, 1, 'last_name', ''),
																			(3, 1, 'nickname', 'admin'),
																			(4, 1, 'description', ''),
																			(5, 1, 'rich_editing', 'true'),
																			(6, 1, 'comment_shortcuts', 'false'),
																			(7, 1, 'admin_color', 'fresh'),
																			(8, 1, 'use_ssl', '0'),
																			(9, 1, 'show_admin_bar_front', 'true'),
																			(10, 1, 'wp_capabilities', 'a:1:{s:13:"administrator";b:1;}'),
																			(11, 1, 'wp_user_level', '10'),
																			(12, 1, 'dismissed_wp_pointers', 'wp330_toolbar,wp330_saving_widgets,wp340_choose_image_from_library,wp340_customize_current_theme_link,wp350_media,wp360_revisions,wp360_locks'),
																			(13, 1, 'show_welcome_panel', '1'),
																			(14, 1, 'wp_dashboard_quick_press_last_post_id', '3');

							 		*/
	        }// end admintrator

	if ($_POST['role'] == 'contributor') 
	{
		//echo '<h1>insert contributor thành công</h1>';
					$name_full_user = $_POST['first_name'].$_POST['last_name'];
							$user_registered = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
							$pass_p = hask_password($_POST['pass1']);

							 $username = $_POST['user_login'];
					       if ( username_exists( $username ) and email_exists($_POST['email']) )
					       {
					           echo "Tên hoặc email này đã có người sử dụng!"; return;
					       }else
					       	{  
					       		
					       		$wpdb->insert( 
										'wp_users', 
										array( 
										'user_login' => $_POST['user_login'],
										'user_pass' => $pass_p,
										'user_nicename' => $_POST['user_login'],
										'user_email' => $_POST['email'],
										'user_url' => $_POST['url'],
										'user_registered' => $user_registered,
										'user_activation_key' => null,
										'user_status' => 0,
										'display_name' => $name_full_user,
										), 
										array( 
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
										) 
								);

					       // lay ID user vừa chen vào 
							// chen ID user vao bang wp_usermeta					
								$user = get_user_by( 'email', $_POST['email'] );
						        $user_id= $user->ID ;

						        //echo 'them 1 nguoi dung';
						        //var_dump($user_id);
						        //return;

						     // insert trruong du lieu ( wp_capabilities ) 1 :12 
							$wp_capabilities = 'wp_capabilities';
							$wp_capabilities_value = 'a:1:{s:11:"contributor";b:1;}';
							$wp_user_level = 'wp_user_level';
							$wp_user_level_val = '1';

							$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
									VALUES (%d, %d, %s, %s ) ", 
							        array(
							        	NULL,
									$user_id, 
									$wp_capabilities,
									$wp_capabilities_value ) 
							    ) );

							$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
									VALUES (%d, %d, %s, %s ) ", 
							        array(
							        	NULL,
									$user_id, 
									$wp_user_level,
									$wp_user_level_val ) 
							    ) );
							
							   // update lai lan nua cho chac an
							/*
								$wpdb->query( " UPDATE wp_usermeta 
												WHERE wp_usermeta.user_id = wp_users.ID "
											);
							*/
						echo "<h1>Thêm người dùng thành công</h1>";
						return;
			}//elseif($_POST['email']) 

	}//if ($_POST['role'] == 'contributor') 

	if ($_POST['role'] == 'editor') 
	{
		//echo '<h1>insert editor thành công</h1>';
		$name_full_user = $_POST['first_name'].$_POST['last_name'];
							$user_registered = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
							$pass_p = hask_password($_POST['pass1']);

							 $username = $_POST['user_login'];
					       if ( username_exists( $username ) and email_exists($_POST['email']) )
					       {
					           echo "Tên hoặc email này đã có người sử dụng!"; return;
					       }else
					       	{  
					       		
					       		$wpdb->insert( 
										'wp_users', 
										array( 
										'user_login' => $_POST['user_login'],
										'user_pass' => $pass_p,
										'user_nicename' => $_POST['user_login'],
										'user_email' => $_POST['email'],
										'user_url' => $_POST['url'],
										'user_registered' => $user_registered,
										'user_activation_key' => null,
										'user_status' => 0,
										'display_name' => $name_full_user,
										), 
										array( 
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
											'%s',
										) 
								);

						       // lay ID user vừa chen vào 
								// chen ID user vao bang wp_usermeta					
									$user = get_user_by( 'email', $_POST['email'] );
							        $user_id= $user->ID ;

							     // insert trruong du lieu ( wp_capabilities ) 1 :12 
								$wp_capabilities = 'wp_capabilities';
								$wp_capabilities_value = 'a:1:{s:6:"editor";b:1;}';
								$wp_user_level = 'wp_user_level';
								$wp_user_level_val = '7';

								$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
										VALUES (%d, %d, %s, %s ) ", 
								        array(
								        	NULL,
										$user_id, 
										$wp_capabilities,
										$wp_capabilities_value ) 
								    ) );

								$wpdb->query($wpdb->prepare( " INSERT INTO wp_usermeta ( umeta_id,user_id, meta_key, meta_value )
										VALUES (%d, %d, %s, %s ) ", 
								        array(
								        	NULL,
										$user_id, 
										$wp_user_level,
										$wp_user_level_val ) 
								    ) );
								
								   // update lai lan nua cho chac an
								/*

									$wpdb->query( " UPDATE wp_usermeta 
													WHERE wp_usermeta.user_id = wp_users.ID "
												);
								*/
							echo "<h1>Thêm người dùng thành công</h1>";
							return;
			                }//elseif($_POST['email']) 

		                
	} //if ($_POST['role'] == 'editor') 

  }//if ($_POST) 
				 	
}//if (isset($_POST['createuser']) ?>

<?php } //elseif ( ! current_user_can( 'list_users' ) )  ?>